package com.binance.mbxgateway.web.service.impl;

import org.apache.commons.codec.binary.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpStatusCodeException;

import com.binance.account.vo.user.response.GetUserResponse;
import com.binance.master.error.BusinessException;
import com.binance.master.error.GeneralCode;
import com.binance.mbxgateway.vo.response.PingSteamResponse;
import com.binance.mbxgateway.vo.response.StartStreamResponse;
import com.binance.mbxgateway.web.service.IMatchboxService;
import com.binance.mbxgateway.web.service.IUserStreamService;
import com.binance.mbxgateway.web.utils.Constants;
import com.binance.mbxgateway.web.utils.MatchboxReturnUtils;
import com.binance.sysconf.service.SysConfigVarCacheService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class UserStreamServiceImpl extends BaseServiceImpl implements IUserStreamService {

    @Autowired
    private IMatchboxService matchboxService;
    @Autowired
    private SysConfigVarCacheService sysConfigService;

    @Override
    public StartStreamResponse startStream(String userId) throws Exception {
        GetUserResponse userResponse = this.getCheckedUser(userId);
        try {
            String json = this.matchboxService.postUserDataStream(userResponse.getUserInfo().getTradingAccount());
            return MatchboxReturnUtils.getMbxValue(json, StartStreamResponse.class);
        } catch (HttpStatusCodeException e) {
            String errJson = e.getResponseBodyAsString();
            log.error("startStream is error:{},{}", errJson, e);
            throw new BusinessException(errJson);
        } catch (Exception e) {
            log.error("startStream exception: {}", e);
            throw e;
        }
    }

    @Override
    public StartStreamResponse internalStartStream(String userId) throws Exception {
        if (StringUtils.equals("1", this.sysConfigService.getValue(Constants.SYSTEM_MAINTENANCE))) {
            throw new BusinessException(GeneralCode.SYS_MAINTENANCE);
        }
        return this.startStream(userId);
    }

    @Override
    public PingSteamResponse pingStream(String userId) throws Exception {
        if (StringUtils.equals("1", this.sysConfigService.getValue(Constants.SYSTEM_MAINTENANCE))) {
            throw new BusinessException(GeneralCode.SYS_MAINTENANCE);
        }
        GetUserResponse userResponse = this.getCheckedUser(userId);
        try {
            String json = this.matchboxService.postUserDataStream(userResponse.getUserInfo().getTradingAccount());
            return MatchboxReturnUtils.getMbxValue(json, PingSteamResponse.class);
        } catch (HttpStatusCodeException e) {
            String errJson = e.getResponseBodyAsString();
            log.error("pingStream is error:{},{}", errJson, e);
            throw new BusinessException(errJson);
        } catch (Exception e) {
            log.error("pingStream exception: {}", e);
            throw e;
        }
    }

    @Override
    public void internalPingStream(String listenKey) throws Exception {
        try {
            this.matchboxService.putUserDataStream(listenKey);
        } catch (HttpStatusCodeException e) {
            String errJson = e.getResponseBodyAsString();
            log.error("internalPingStream is error:{},{}", errJson, e);
            throw new BusinessException(errJson);
        } catch (Exception e) {
            log.error("internalPingStream exception: {}", e);
            throw e;
        }
    }
}
